import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import '../../../../config/GlobalConfig.dart';
import '../../../../res/HDColors.dart';
import '../../../../util/LoadingUtils.dart';
import '../../../../util/location/location_gaode.dart';
import '../../../../view/FieldDate.dart';
import '../../../../view/FieldEditor.dart';
import '../../../../view/common_widget.dart';
import '../../../../view/image_picker_widget.dart';
import '../base_msg/logic.dart';
import 'logic.dart';

class ExploreMsgPage extends StatelessWidget {
  final logic = Get.put(ExploreMsgLogic());
  final state = Get.find<ExploreMsgLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BaseMsgLogic>(builder: (logic) {
      return SingleChildScrollView(
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          FieldDate(
              label: '踏勘时间',
              initialValue: state.rnInfo.surveyDate ?? "",
              readOnly: state.isReadOnly,
              minDateTime: DateFormat("yyyy-MM-dd HH:mm:ss").format(
                  DateTime.now().add(const Duration(days: -365, minutes: 1))),
              onChanged: (value) {
                state.rnInfo.surveyDate = value;
              }),
          FieldEditor(
              label: '踏勘人员',
              contentTextAlign: TextAlign.end,
              initialValue: state.rnInfo.userName,
              showLine: false,
              readOnly: state.isReadOnly,
              controller: state.rnNameController
                ..text = (state.rnInfo.userName ??
                        GlobalConfig.loginEntity?.userName) ??
                    "",
              onChanged: (value) {
                state.rnInfo.userName = value;
              }),
          FieldEditor(
              label: '踏勘人员电话',
              hintText: '请输入手机号',
              contentTextAlign: TextAlign.end,
              initialValue: state.rnInfo.phoneNumber,
              showLine: false,
              readOnly: state.isReadOnly,
              controller: state.rnPhoneController
                ..text = state.rnInfo.phoneNumber ?? "",
              onChanged: (value) {
                state.rnInfo.phoneNumber = value;
              }),
          Visibility(
              visible: false,
              child: FieldEditor(
                  label: '踏勘方式',
                  hintText: '请输入踏勘方式',
                  initialValue: state.rnInfo.surveyMethod,
                  contentTextAlign: TextAlign.end,
                  showLine: false,
                  readOnly: state.isReadOnly,
                  controller: state.rnTypeController
                    ..text = state.rnInfo.surveyMethod ?? "人工踏勘",
                  onChanged: (value) {
                    state.rnInfo.surveyMethod = value;
                  })),
          Container(
            color: HDColors.white,
            child: Stack(children: [
              FieldEditor(
                  label: '踏勘位置',
                  hintText: '请输入踏勘位置',
                  contentTextAlign: TextAlign.end,
                  initialValue: state.rnInfo.address,
                  rightIcon:
                      Icon(color: HDColors.blue, Icons.location_on_outlined),
                  showLine: false,
                  readOnly: state.isReadOnly,
                  controller: state.rnLocationController
                    ..text = state.rnInfo.address ?? "",
                  onChanged: (value) {
                    state.rnInfo.address = value;
                  }),
              Positioned(
                right: -10,
                top: 0,
                bottom: 0,
                child: TextButton(
                  onPressed: () async {
                    LoadingUtils.showToast("开始获取当前定位");

                    var getLocation =
                        await LocationGaode().startLocation((location) {
                      if ((location.address ?? "").isEmpty) {
                        LoadingUtils.showToast("定位失败,请重新定位");
                      } else {}
                    });
                    LoadingUtils.showToast("定位:${getLocation.address}");
                    state.rnInfo.address = getLocation.address;
                    state.rnInfo.addressLongitude = getLocation.longitude;
                    state.rnInfo.addressLatitude = getLocation.latitude;
                    state.rnLocationController.text =
                        state.rnInfo.address ?? "";
                    logic.update();
                  },
                  child: Container(
                    alignment: Alignment.centerRight,
                    height: 80,
                    width: 30,
                  ),
                ),
              ),
            ]),
          ),
          Visibility(
            visible: (state.rnInfo.addressLongitude ?? "").isNotEmpty,
            child: BrnTextInputFormItem(
              title: '经度：${state.rnInfo.addressLongitude}',
              isRequire: false,
              isEdit: false,
              hint: "",
            ),
          ),
          Visibility(
            visible: (state.rnInfo.addressLatitude ?? "").isNotEmpty,
            child: BrnTextInputFormItem(
              title: '纬度：${state.rnInfo.addressLatitude}',
              isRequire: false,
              isEdit: false,
              hint: "",
            ),
          ),
          FieldEditor(
              label: '台区名称',
              hintText: '请输入台区名称',
              initialValue: state.rnInfo.areaName,
              contentTextAlign: TextAlign.end,
              showLine: false,
              readOnly: state.isReadOnly,
              controller: state.areaNameController
                ..text = state.rnInfo.areaName ?? "",
              onChanged: (value) {
                state.rnInfo.areaName = value;
              }),
          FieldEditor(
              label: '台区容量',
              hintText: '请输入台区容量',
              initialValue: state.rnInfo.areaCapacity,
              contentTextAlign: TextAlign.end,
              showLine: false,
              readOnly: state.isReadOnly,
              controller: state.areaCapacityController
                ..text = state.rnInfo.areaCapacity ?? "",
              onChanged: (value) {
                state.rnInfo.areaCapacity = value;
              }),
          FieldEditor(
              label: '台区距离',
              hintText: '请输入台区距离',
              initialValue: state.rnInfo.areaDistance,
              contentTextAlign: TextAlign.end,
              showLine: false,
              readOnly: state.isReadOnly,
              controller: state.areaDistanceController
                ..text = state.rnInfo.areaDistance ?? "",
              onChanged: (value) {
                state.rnInfo.areaDistance = value;
              }),
          FieldEditor(
              label: '备注',
              hintText: '请输入备注信息',
              initialValue: state.rnInfo.remarks,
              contentTextAlign: TextAlign.end,
              showLine: false,
              isRequired: false,
              readOnly: state.isReadOnly,
              controller: state.rnRemarkController
                ..text = state.rnInfo.remarks ?? "",
              onChanged: (value) {
                state.rnInfo.remarks = value;
              }),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('勘测单',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.surveySheetController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('草图',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.roughSketchController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('俯视全景图',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.panoramicPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('建筑正面全景图',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.rnGatePhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('内部细节照片(对角与仰拍天花板)',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.oppositeAnglesPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('外部细节照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.externalDetailPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('其他',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.rnOtherImgController),
          ),
          Visibility(visible: !state.isReadOnly, child: getFooterButton())
        ]),
      );
    });
  }

  getFooterButton() {
    return Container(
        decoration: const BoxDecoration(color: HDColors.white, boxShadow: [
          BoxShadow(
              color: HDColors.grayBg,
              offset: Offset(0, -2),
              blurRadius: 8,
              spreadRadius: 1)
        ]),
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            CommonWidget.buildBottomBtn("暂存", () {
              logic.save();
            }),
            CommonWidget.buildBottomBtn("提交", () {
              logic.submit(1);
            }),
          ],
        ));
  }
}
